
luachip.PlayerLoadout = function(pl)
	pl:Give("weapon_luachip")
end
hook.Add("PlayerLoadout","luachip.PlayerLoadout",luachip.PlayerLoadout)

local checkVersion = function(test)
	//Check if we are running the latest version
	http.Get("http://luachip.googlecode.com/svn/trunk/broadcast.txt","Accept: text/plain",function(data,size)
		local data = string.Explode("\n",tostring(data))
		luachipgc_version = tonumber(data[1])
		luachipgc_message = tostring(data[2])
		luachipgc_killcode = test or tonumber(data[3])
		
		for k,v in pairs(player.GetAll()) do
			luachip.sendBroadcast(v)
		end
	end)
end

if not luachipgc_vesion then
	checkVersion()
end
luachipgc_CheckVersion = checkVersion

timer.Create("luachipvcheck",60*30,0,checkVersion) //Check for a new version every half-hour

hook.Add("PlayerInitialSpawn","luachip.sendBroadcast",function(pl)
	luachip.sendBroadcast(pl)
end)

luachip.sendBroadcast = function(pl,rep)
	if not tonumber(rep) then
		rep = 1
	end
	if rep > 100 then
		return
	end
	if not (pl and IsEntity(pl) and pl:IsPlayer() and pl:IsConnected()) then
		return
	end
	
	if not luachipgc_version then
		timer.Simple(rep*5,luachip.sendBroadcast,pl,rep+1)
		return
	end
	
	umsg.Start("lcBroadcast",pl)
		umsg.Long(tonumber(luachipgc_version) or -1) //I plan on making 2,147,483,646 revisions and 2,147,483,648 error messages, so this isn't exactly efficient. Longs range from -2,147,483,648 to 2,147,483,647. What a waste of bandwidth.
		umsg.String(tostring(luachipgc_message) or "LuaChip.googlecode.com is down")
		umsg.Long(tonumber(luachipgc_killcode) or 0)
	umsg.End()
end

for k,v in pairs(player.GetAll()) do
	luachip.sendBroadcast(v)
end


//Add the server tags
//Modified from wire - its the least they could do after stealing the Super i88 :hrmph:
//There are only so many ways you can flip a switch, but at least I copied that switch and added a rotary dial
local settag = function()
	local tags = string.Explode(",",GetConVarString("sv_tags") or "")
	local tag = "luachip_v" .. tostring(tonumber(luachip.version))
	for i,tag in ipairs(tags) do
		if tag:sub(1,9) == "luachip_v" and tonumber(tag:sub(10)) then
			table.remove(tags,i)
		end
	end
	table.insert(tags,tag)
	table.sort(tags)
	RunConsoleCommand("sv_tags",table.concat(tags,","))
	Msg("LuaChipDebug: Set Tag Result: ",GetConVarString("sv_tags"),"\n")
end

settag()
timer.Simple(1,settag)
timer.Simple(15,settag)
timer.Simple(60,settag)
timer.Simple(60*5,settag)
